﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagement
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        //显示数据
        private void Login_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var dt = Means.GetTheData(sql);
            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }
        //查询
        private void QueryButton_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Title like '%{0}%'",name);
            var dt = Means.GetTheData(sql);
            dataGridView1.DataSource = dt;
        }
        //添加
        private void AddButton_Click(object sender, EventArgs e)
        {
            AddAndDelect addAndDelect = new AddAndDelect();
            var res = addAndDelect.ShowDialog();

                var name = textBox1.Text;
                var sql = string.Format("select * from Articles where Title like '%{0}%'", name);
                var dt= Means.GetTheData(sql);
                dataGridView1.DataSource = dt;


            
        }

        //修改
        private void ModifyTheButton_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var createdtime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                


                AddAndDelect addAnd = new AddAndDelect(id, title, author, createdtime);
                var res = addAnd.ShowDialog();


                    var names = textBox1.Text;
                    var sql = string.Format("select * from Articles where Title like '%{0}%'", names);
                    var dt = Means.GetTheData(sql);
                    dataGridView1.DataSource = dt;

                if (res == DialogResult.Yes)
                {
                    MessageBox.Show("修改成功！", "提示");
                }
                else
                {
                    
                }

            }
            else
            {
                MessageBox.Show("还未选择要修改的值！憨憨");
            }

        }
        //删除
        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {

                
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("确认删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);


                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);

                    var rowcount = Means.AddorEditOrDeleteSave(sql);


                        var name = textBox1.Text;
                        var sqls = string.Format("select * from Articles where Title like '%{0}%'", name);
                        var dt = Means.GetTheData(sqls);
                        dataGridView1.DataSource = dt;

                    if (rowcount > 0)
                    {
         
                        MessageBox.Show("删除成功", "提示");


                    }

                }
            }
            else
            {
                MessageBox.Show("没有选择数据", "提示");
            }
        }

        private void LogOutButton_Click(object sender, EventArgs e)
        {
            this.Close();
            Form1 form1 = new Form1();
            form1.Show();
        }

        private void label2_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
               
        }
    }
}
